package xunlei;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import swing2swt.layout.FlowLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/*
 *@author 邓强
 *@软件2班
 */
public class XunLei {

	protected Shell shell;
	private Text text;
	private Table table;
	private Button button;
	private Map<String, Task> taskMap = new HashMap<String, Task>();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			XunLei window = new XunLei();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(688, 434);
		shell.setText("迅雷下载");
		shell.setLayout(null);

		SashForm sashForm = new SashForm(shell, SWT.NONE);
		sashForm.setBounds(0, 0, 672, 396);
		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setLayout(null);

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(10, 20, 61, 17);
		label.setText("下载地址：");

		text = new Text(composite, SWT.BORDER);
		text.setBounds(72, 14, 451, 29);

		button = new Button(composite, SWT.NONE);

		button.setBounds(559, 14, 80, 27);
		button.setText("下载");

		Composite composite_1 = new Composite(sashForm, SWT.NONE);

		table = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(0, 0, 672, 315);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("文件名");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(193);
		tableColumn_1.setText("下载地址");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(183);
		tableColumn_2.setText("保存路径");

		TableColumn tableColumn_3 = new TableColumn(table, SWT.CENTER);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("下载进度");

		TableColumn tableColumn_4 = new TableColumn(table, SWT.CENTER);
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("下载状态");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);

		Menu menu = new Menu(tableCursor);
		tableCursor.setMenu(menu);

		MenuItem menuItem = new MenuItem(menu, SWT.NONE);

		menuItem.setText("暂停");

		MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);

		menuItem_1.setText("启动");
		MenuItem menuItem_2 = new MenuItem(menu, SWT.NONE);
		menuItem_2.setText("删除");
		sashForm.setWeights(new int[] {57, 336});
		
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String url = text.getText().trim();

				if(url == null || url.equals("")) {
					return;
				}

				DirectoryDialog dd = new DirectoryDialog(shell, SWT.SAVE);
				dd.setText("保存路径");
				dd.setMessage("请选择你要保存的文件路径");
				dd.setFilterPath("SystemDrive");
				String path = dd.open();

				if(path == null || path.equals("")) {
					return;
				}

				//启动一个下载任务
				Task task = new Task(url, path, table);
				taskMap.put(url.substring(url.lastIndexOf("/") + 1), task);
				new Thread(task).start();
			}
		});
		
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();
				Task task = taskMap.get(ti.getText(0));
				task.pause = false;
				task.start();
				menuItem.setEnabled(true);
				menuItem_1.setEnabled(false);
			}
		});
		
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();
				Task task = taskMap.get(ti.getText(0));
				task.pause = true;
				menuItem.setEnabled(false);
				menuItem_1.setEnabled(true);
			}
		});
	}
}
